
import Mock from "mockjs"

//费用管理
var data = {}
if (localStorage.getItem("user_money_list")) {
    data = JSON.parse(localStorage.getItem('user_money_list'))
} else {
    data = Mock.mock({
        "list|50": [
            {
                id: "@id",
                type: "招待费",
                "money|500-1000": 1,
                name: "@cname",
                stus: "未报销",
                date: "2022-5.20",
            }
        ]
    })
    //保存数据到本地存储
    localStorage.setItem('user_money_list', JSON.stringify(data))
}
//获取人员列表
Mock.mock('/api/user_money/list', "get", () => {
    return data
})
//详情
Mock.mock('/api/user_money/detail', "post", (req) => {
    var { id } = JSON.parse(req.body)
    var i = data.list.findIndex((item) => {
        return item.id == id
    })
    if (i != -1) {
        return data.list[i]
    }
})
//删除
Mock.mock('/api/user_money/delete', 'post', (req) => {
    //接收参数，req.body是字符串格式，需要转为json对象
    var { id } = JSON.parse(req.body);
    var index = data.list.findIndex((item) => {
        return item.id == id;
    })
    if (index != -1) {
        data.list.splice(index, 1);
        localStorage.setItem('user_money_list', JSON.stringify(data))
    }
    return { status: 200, msg: '删除成功', list: data.list }
})

//修改
Mock.mock("/api/user_money/change", "post", (req) => {
    var { id, name, type, stus, date, money } = JSON.parse(req.body);
    // console.log(id, name, type, stus, date, money);
    let i = data.list.findIndex((item) => {
        return item.id == id;
    })
    if (i != -1) {
        data.list.splice(i, 1, { id, name, type, stus, date, money })
        localStorage.setItem('user_money_list', JSON.stringify(data))
    }
})

//增加
Mock.mock("/api/user_money/personadd", "post", (req) => {

    var { id, name, date, money, type, stus } = JSON.parse(req.body);
    // console.log(req.body);
    id = Math.round(Math.random() * 1000000000000000000)
    let i = data.list.findIndex((item) => {
        return item.id == id;

    })
    console.log(data.list);
    if (i == -1) {
        var obj = {
            id: id,
            name: name,
            date: date,
            money: money,
            stus: "未报销",
            type: "招待费"
        }
        data.list.unshift(obj)
        localStorage.setItem('user_money_list', JSON.stringify(data))
    }
})

//搜索
Mock.mock("/api/user_money/search", "post", (req) => {
    var { name } = JSON.parse(req.body);
    console.log(name);
    var obj = data.list.find((item) => {
        return item.name == name;
    })
    if (obj) {
        return { code: 1, msg: "查询成功！", obj }
    } else {
        return { code: 0, msg: "查询失败" }
    }

})


